package com.ctrip.task;

import com.alibaba.fastjson.JSONArray;
import com.ctrip.business.PCOrderService;
import com.ctrip.constant.OrderStatusEnum;
import com.ctrip.utils.Jacob4TTS;
import com.go.commons.dto.Page;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;

@Slf4j
public class OrderTask {
    private static final int INTERVAL_MINUTES = 5;
    private static boolean start;

    public static void start() {
        start=true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (start) {
                    JSONArray orders = PCOrderService.listOrders(new Page(10, 1), OrderStatusEnum.WAITING);
                    if (orders.size() > 0) {
                        String msg = String.format("发现有%d条待抢单", orders.size());
                        Jacob4TTS.speak(msg);
                        JOptionPane.showMessageDialog(null, msg,"待抢单提醒", JOptionPane.INFORMATION_MESSAGE);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            log.error(e.toString(),e);
                        }
                        //线程池处理
//                    for (int i = 0; i < orders.size(); i++) {
//                        JSONObject order = orders.getJSONObject(i);
//
//                    }
                    }
                }
            }
        }).start();
    }

    public static void stop() {
        start=false;
    }
}
